package org.gw.building.ctrl;

import org.apache.commons.collections.CollectionUtils;
import org.gw.building.model.Bm;
import org.gw.building.model.Yx;
import org.gw.building.model.YxBm;
import org.gw.building.repositories.YxBmRepository;
import org.gw.building.repositories.YxRepository;
import org.gw.ylc.base.context.RequestContextHolder;
import org.gw.ylc.base.interceptor.RequestPermission;
import org.gw.ylc.base.util.PermissionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author qianwx
 */
@RequestMapping("tqtjsz")
@RestController
public class YxCtrl {

    @Autowired
    private YxRepository yxRepository;

    @Autowired
    private YxBmRepository yxBmRepository;

    @RequestMapping("list")
    @RequestPermission({RequestPermission.SCHOOL})
    public Page<Yx> list(String mc, String sfyx) {
        Page<Yx> yxs = yxRepository.listYx(PermissionUtil.toLikeString(mc), sfyx, RequestContextHolder.get().getPageable());
        if (yxs.getSize() > 0) {
            List<Yx> yxList = yxs.getContent();
            yxList.forEach(yx -> {
                List<YxBm> yxbms = yxBmRepository.findByYx(yx);
                if (CollectionUtils.isNotEmpty(yxbms)) {
                    List<Bm> bms = new ArrayList<>();
                    String[] ssyx = new String[yxbms.size()];
                    for (int i = 0; i < yxbms.size(); i++) {
                        YxBm yxBm = yxbms.get(i);
                        bms.add(yxBm.getBm());
                        ssyx[i] = yxBm.getBm().getId();
                    }
                    yx.setBms(bms);
                    yx.setSsyx(ssyx);
                }
            });
        }
        return yxs;
    }

    @RequestMapping("px")
    @RequestPermission({RequestPermission.SCHOOL})
    public int px() {
        Integer px = yxRepository.maxPx();
        return px == null ? 1 : px + 10;
    }

    @RequestMapping("addYx")
    @RequestPermission({RequestPermission.SCHOOL})
    @Transactional
    public void addYx(Yx yx) {
        String[] bmArr = yx.getSsyx();
        final Yx _yx = yxRepository.save(yx);
        if (bmArr != null && bmArr.length > 0) {
            yxBmRepository.deleteByYx(_yx);
            final List<YxBm> yxbms = new ArrayList<>();
            Arrays.stream(bmArr).forEach(s -> {
                YxBm yxBm = new YxBm();
                yxBm.setYx(_yx);
                yxBm.setBm(new Bm(s));
                yxbms.add(yxBm);
            });
            yxBmRepository.save(yxbms);
        }

    }

    @RequestMapping("delete")
    @RequestPermission({RequestPermission.SCHOOL})
    @Transactional
    public void delete(Yx yx) {
        yxBmRepository.deleteByYx(yx);
        yxRepository.delete(yx);
    }

    @RequestMapping("deleteYxs")
    @RequestPermission({RequestPermission.SCHOOL})
    @Transactional
    public void delete(List<Yx> yxs) {
        yxs.forEach(yx ->
                yxBmRepository.deleteByYx(yx)
        );
        yxRepository.delete(yxs);
    }
}
